<?php 
/*
 module:		留言管理
 create_time:	2022-03-07 19:27:21
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;

use app\api\service\ZhMessageService;
use app\api\model\ZhMessage as ZhMessageModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use app\backend\model\SysSite as SysSiteModel;
use app\api\model\Member as MemberModel;

class ZhMessage extends Common {


	/**
	* @api {get} /ZhMessage/view 01、查看详情
	* @apiGroup ZhMessage
	* @apiVersion 1.0.0
	* @apiDescription  查看详情
	
	* @apiParam (输入参数：) {string}     		message_id 主键ID

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据详情
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":"201","msg":"没有数据"}
	*/
	function view(){
        $wxapp_id = $this->request->get('wxapp_id');
        $uId      = $this->request->uid;
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }
        
        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if(!$user){
            return $this->ajaxReturn($this->errorCode,'未知用户');
        }
		$messageId = $this->request->get('message_id','','serach_in');
        if(!$messageId){
            return $this->ajaxReturn($this->errorCode,'缺少信息参数');
        }
        $data = [
            'wxapp_id'  => $wxapp_id,
            'u_id'      => $uId,
            'message_id'=> $messageId
        ];
		$field='*';
		$res  = checkData(ZhMessageModel::field($field)->where($data)->find());
		$res['createtime'] = date('Y-m-d H:i:s',$res['createtime']);
		return $this->ajaxReturn($this->successCode,'返回成功',$res);
	}

	/**
	* @api {post} /ZhMessage/add 02、添加
	* @apiGroup ZhMessage
	* @apiVersion 1.0.0
	* @apiDescription  添加

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"
	* @apiParam (输入参数：) {string}			wxapp_id 平台id 
	* @apiParam (输入参数：) {string}			content 内容 
	* @apiParam (输入参数：) {int}				status 状态 待审核|0|success,通过|1|warning,拒绝|2|info

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"操作失败"}
	*/
	function add(){
        $wxapp_id = $this->request->post('wxapp_id');
        $uId      = $this->request->uid;
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }
        
        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if(!$user){
            return $this->ajaxReturn($this->errorCode,'未知用户');
        }
        
		$postField          = 'content,images';
		$data               = $this->request->only(explode(',',$postField),'post',null);
		if(!$data['content']){
            return $this->ajaxReturn($this->errorCode,'缺少信息数据');
        }
		$data['wxapp_id']   = $wxapp_id;
		$data['u_id']       = $uId;
		$data['createtime'] = time();
		$data['status']     = 0;
		
		$res = ZhMessageService::add($data);
		return $this->ajaxReturn($this->successCode,'操作成功',$res);
	}



}

